/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.ui.base.BaseActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Activity;
import com.simon.mengkou.data.bean.base.User;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.view.AvatarSimpleDraweeView;
import com.simon.mengkou.ui.view.FollowButton;
import com.simon.mengkou.utils.UtilOuer;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/10.
 * desc    : 通用关注适配器
 */
public class CommonFollowAdapter extends AbsAdapter<User> {
    private BaseActivity mActivity;

    public CommonFollowAdapter(BaseActivity context) {
        super(context);
        mActivity = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        final Holder holder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.layout_common_follow_item, null);
            holder = new Holder();
            convertView.setTag(holder);
            ButterKnife.bind(holder, convertView);
        } else {
            holder = (Holder) convertView.getTag();
        }

        final User data = getItem(position);
        //打开用户详情
        holder.mLlRoot.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

            }
        });
        //头像
        holder.mSdvAvatar.loadAvatar(data.getAvatarUrl(), data.getLevel());
        holder.mSdvAvatar.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                OuerDispatcher.presentUserActivity(mContext, data.getUserId(), data.getNick());
            }
        });

        //标题
        holder.mTvName.setText(data.getNick());
        //时间
        holder.mTvTime.setText(UtilOuer.getFormatDate(mContext, data.getCreatedAt(), "yy-M-d"));
        //关注
        holder.mFbFollow.follow(data.getInterested());
        holder.mFbFollow.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                data.setInterested(!data.getInterested());
                holder.mFbFollow.follow(data.getInterested());

                if(data.getInterested()) {
                    OuerApplication.mUser.setFollowCount(OuerApplication.mUser.getFollowCount() + 1);
                } else {
                    OuerApplication.mUser.setFollowCount(OuerApplication.mUser.getFollowCount() - 1);
                }

                //调用关注or取消关注接口
                AgnettyFuture future = OuerApplication.mOuerFuture.follow(
                        OuerApplication.mUser.getUserId(),
                        data.getUserId(),
                        data.getInterested(), null
                );

                mActivity.attachDestroyFutures(future);

                //发送关注or取消关注广播
                OuerDispatcher.sendFollowBroadcast(mActivity, data.getUserId(),
                        data.getInterested());
            }
        });
        return convertView;
    }


    /**
     * @author zhenshui.xia
     */
    class Holder {
        //
        @Bind(R.id.follow_id_root)
        LinearLayout mLlRoot;
        //头像
        @Bind(R.id.follow_id_avatar)
        AvatarSimpleDraweeView mSdvAvatar;
        //标题
        @Bind(R.id.follow_id_name)
        TextView mTvName;
        //时间
        @Bind(R.id.follow_id_time)
        TextView mTvTime;
        //关注
        @Bind(R.id.follow_id_btn)
        FollowButton mFbFollow;
    }
}
